package com.example.demospringboot;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author leiyu
 */
@Slf4j
@SpringBootApplication
public class DemoSpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoSpringbootApplication.class, args);
        log.info("启动时间：{} xxx", LocalDateTime.now());
        System.out.printf("启动时间：%s xxx", LocalDateTime.now());
    }

    /**
     * <a href="http://localhost:8000/demo1/view1">...</a>
     */
    @Controller
    @RequestMapping("demo1")
    public static class HelloController {

        @RequestMapping("/view1")
        public String view1() {
            return "index1";
        }

        @RequestMapping("/view2")
        public ModelAndView view2() {
            log.info("Controller 收到请求");
            ModelAndView mv = new ModelAndView();
            mv.addObject("msg", "你好");
            // 设置对应网页的名字
            mv.setViewName("index2");
            return mv;
        }

        @GetMapping("/hello")
        @ResponseBody
        public String hello() {
            log.info("Controller 收到请求");
            return "world";
        }
    }

    /**
     * RestController = Controller + ResponseBody
     */
    @RestController
    @RequestMapping("demo2")
    public static class HelloRestController {
        @GetMapping("/hello")
        public String hello() {
            log.info("RestController 收到请求");
            return "world";
        }

        @GetMapping("/hello2")
        public Map<String, String> hello2() {
            log.info("RestController 收到请求");
            return Map.of("k1", "v1", "k2", "v2");
        }
    }


}
